
package com.android.formula.task;

import android.content.Context;

import com.android.formula.common.MyApplication;
import com.android.formula.dao.FormulaNoteDao;
import com.android.formula.dao.NoteSubjectDao;
import com.android.formula.dao.TypeFormulaNoteDao;
import com.android.formula.database.NoteDatabaseConnection;
import com.android.formula.model.FormulaNote;
import com.android.formula.model.NoteSubject;
import com.android.formula.model.TypeFormula;
import com.android.formula.utils.Constants;

public class DeleteNoteSubjectDBTask extends BaseDatabaseTask<NoteSubject, String> {

    public DeleteNoteSubjectDBTask(Context context, NoteSubject param, Class<String> responType,
            IDataEventHandler<String> dataEventHandler) {
        super(context, param, responType, dataEventHandler);
    }

    @Override
    protected String execute() {
        NoteDatabaseConnection dbcn = MyApplication.getDatabaseConnection();
        if (dbcn == null || mRequestParams == null) {
            return null;
        }

        dbcn.open();
        NoteSubjectDao subjectDao = new NoteSubjectDao(NoteSubject.class, mContext,
                dbcn.getDatabase());
        subjectDao.delete("" + mRequestParams.getSubjectId());

        TypeFormulaNoteDao typeDao = new TypeFormulaNoteDao(TypeFormula.class, mContext,
                dbcn.getDatabase());
        typeDao.deleteFromColumn(Constants.TABLE_TYPE_FORMULA_COL_TYPE_SUBJECTID, ""
                + mRequestParams.getSubjectId());

        FormulaNoteDao formulaDao = new FormulaNoteDao(FormulaNote.class, mContext,
                dbcn.getDatabase());
        formulaDao.deleteFormulaFromSubject(mRequestParams.getSubjectId());

        dbcn.close();
        return "";
    }
}
